package com.meida.module.bus.provider.service.impl;

import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.module.bus.client.entity.BusReceiptDeliveryRecordBack;
import com.meida.module.bus.provider.api.util.ApiConstants;
import com.meida.module.bus.provider.api.util.PmsApiUtil;
import com.meida.module.bus.provider.mapper.BusReceiptDeliveryRecordBackMapper;
import com.meida.module.bus.provider.service.BusRecDeliBkReqConfRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 预留发货冲销申请表接口实现类
 *
 * @author flyme
 * @date 2020-12-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusRecDeliBkReqConfRecordServiceImpl extends BaseServiceImpl<BusReceiptDeliveryRecordBackMapper, BusReceiptDeliveryRecordBack> implements BusRecDeliBkReqConfRecordService {

    @Autowired
    private PmsApiUtil pmsApiUtil;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusReceiptDeliveryRecordBack brdrb, EntityMap extra) {
        return ResultBody.ok();
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusReceiptDeliveryRecordBack> cq, BusReceiptDeliveryRecordBack brdrb, EntityMap requestMap) {
        cq.notIn("syncStatus", ApiConstants.REC_BK_DELIVERY_INT);
        cq.like(BusReceiptDeliveryRecordBack.class, "status");
        cq.like(BusReceiptDeliveryRecordBack.class, "projectCode");
        cq.like(BusReceiptDeliveryRecordBack.class, "projectName");
        cq.like(BusReceiptDeliveryRecordBack.class, "majorCode");
        cq.like(BusReceiptDeliveryRecordBack.class, "majorName");
        cq.like(BusReceiptDeliveryRecordBack.class, "packageNo");
        cq.like(BusReceiptDeliveryRecordBack.class, "packageName");
        cq.like(BusReceiptDeliveryRecordBack.class, "userName");
        cq.like(BusReceiptDeliveryRecordBack.class, "appTime");
        cq.like(BusReceiptDeliveryRecordBack.class, "receivedDate");
        cq.like(BusReceiptDeliveryRecordBack.class, "zid");
        cq.like(BusReceiptDeliveryRecordBack.class, "zitem");
        cq.like(BusReceiptDeliveryRecordBack.class, "budat");
        cq.like(BusReceiptDeliveryRecordBack.class, "bldat");
        cq.like(BusReceiptDeliveryRecordBack.class, "bktxt");
        cq.like(BusReceiptDeliveryRecordBack.class, "supplierCode");
        cq.like(BusReceiptDeliveryRecordBack.class, "supplierName");
        cq.like(BusReceiptDeliveryRecordBack.class, "reserveNo");
        cq.like(BusReceiptDeliveryRecordBack.class, "reserveRow");
        cq.like(BusReceiptDeliveryRecordBack.class, "lgort");
        cq.like(BusReceiptDeliveryRecordBack.class, "ext01");
        cq.like(BusReceiptDeliveryRecordBack.class, "msgtyFromSap");
        cq.like(BusReceiptDeliveryRecordBack.class, "msgtxFromSap");
        cq.like(BusReceiptDeliveryRecordBack.class, "mblnrFromSap");
        cq.like(BusReceiptDeliveryRecordBack.class, "mblppoFromSap");
        cq.like(BusReceiptDeliveryRecordBack.class, "mjahrFromSap");
        List<String> majorCodes = pmsApiUtil.getAuthMajorCodes(OpenHelper.getUserId());
        if(null != majorCodes) {
            cq.in("majorCode", majorCodes);
        }
        List<String> projectCodeS = pmsApiUtil.getAuthProjectIds(OpenHelper.getUserId());
        if(null != projectCodeS) {
            cq.in("projectCode", projectCodeS);
        }
        cq.orderByDesc("brdrb.createTime");
        return ResultBody.ok();
    }

//    @Override
//    public void syncSap(List<BusReceiptDeliveryRecordBack> records) throws Exception {
//        if (pmsApiUtil.sapConnectionForRecCheck()) {
//            return;
//        }
//        Map<String, String> rsMap = SyncSapUtil.syncSapForCharOff(records);
//        records.stream().forEach(recordObj -> {
//            if (ApiConstants.REC_DERVERY_SAP_BACK_ERR.equals(rsMap.get(ApiConstants.MAP_MSGTYFROMSAP))) {
//                recordObj.setSyncStatus(ApiConstants.REC_DELIVERY_ERR);
//                recordObj.setMsgtyFromSap(ApiConstants.REC_DERVERY_SAP_BACK_ERR);
//                recordObj.setMsgtxFromSap(rsMap.get(ApiConstants.MAP_MSGTXFROMSAP));
//            } else {
//                recordObj.setSyncStatus(ApiConstants.REC_DELIVERY_FINISH);
//            }
//        });
//    }
}
